import Vue from 'vue'
import VueRouter from 'vue-router'
// import Test from '../views/Test'
import Login from '../components/Login'
// import LoginA from '../views/LoginA'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/Users'
import Roles from '../components/Roles'

Vue.use(VueRouter)

const routes = [
  // { path: '/test', component: Test }
  { path: '/login', component: Login },
  { path: '/', redirect: '/login' },
  { path: '/home', component: Home, redirect: '/welcome', children: [{ path: '/welcome', component: Welcome }, { path: '/users', component: Users }, { path: '/roles', component: Roles }] }
  // ,{ path: '/loginA', component: LoginA}
]

const router = new VueRouter({
  routes
})

// 前端拦截器 绑到router上
// 前端路径请求的时候先走这个 在去页面
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from 重那个路径跳转而来
  // next 放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // console.log(!tokenStr)
  // token不存在   伪造token
  if (!tokenStr) return next('/login')
  return next()
})

export default router
